package com.ball.web;

import com.alibaba.fastjson.JSONObject;
import com.ball.service.SimpleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 描述：
 *
 * @author : Aberic 【2018/6/4 15:01】
 */
@Controller
@RequestMapping("/simple")
public class SimpleController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private SimpleService simpleService;

    @RequestMapping(value = "/chaincode", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public String chaincode(@RequestBody Map<String, Object> map) {
        String str=simpleService.chainCode(new JSONObject(map));
//        JSONObject jsonObject = JSONObject.parseObject(str);
//        System.out.println(jsonObject.getJSONObject("result").toJSONString());
//        JSONObject jsonObject1=JSONObject.parseObject(jsonObject.getJSONObject("result").toJSONString());
//        System.out.println("###############");
//        System.out.println(jsonObject1.get("uid"));
//        return str;
        JSONObject jsonObject = JSONObject.parseObject(str);
        JSONObject jsonObject1=JSONObject.parseObject(jsonObject.getJSONObject("result").toJSONString());
        logger.info(jsonObject1.toJSONString());
        return jsonObject1.toJSONString();
    }

    @RequestMapping(value = "/trace", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public String trace(@RequestBody Map<String, Object> map) {

        String str=simpleService.trace(new JSONObject(map));
//        JSONObject jsonObject = JSONObject.parseObject(str);
//        System.out.println(jsonObject.toJSONString());
        return str;
//        JSONObject jsonObject = JSONObject.parseObject(str);
//        return JSONObject.parseObject(jsonObject.getJSONObject("result").toJSONString()).toString();
//        JSONObject jsonObject = JSONObject.parseObject(str);
//        logger.info(jsonObject.toJSONString());
//        JSONObject jsonObject1=JSONObject.parseObject(jsonObject.getJSONObject("result").toJSONString());
//        logger.info(jsonObject1.toJSONString());
//        return jsonObject1.toJSONString();
    }

    @RequestMapping(value = "/org/add", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public String addOrg(@RequestBody Map<String, Object> map) {
        return simpleService.addOrg(new JSONObject(map));
    }

    @RequestMapping(value = "/orderer/add", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public String addOrderer(@RequestBody Map<String, Object> map) {
        return simpleService.addOrderer(new JSONObject(map));
    }

    @RequestMapping(value = "/peer/add", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public String addPeer(@RequestBody Map<String, Object> map) {
        return simpleService.addPeer(new JSONObject(map));
    }

}
